package pl.edu.agh.lab2.sr.beans;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class ConfigReader {
	private static final String propertiesFileName = "server.properties";
	private static final String propertiesFilePath = propertiesFileName;
	// private static final String propertiesFilePath = "pl" + File.separator
	// + "edu" + File.separator + "agh" + File.separator + "lab2"
	// + File.separator + "sr" + File.separator + "main" + File.separator
	// + propertiesFileName;

	private static Properties properties = new Properties();
	private static String battleshipServerObjectPath = null;

	public static String getServerObjectPath() {
		if (battleshipServerObjectPath == null) {
			initBattleshipServerObjectPath();
		}
		return battleshipServerObjectPath;

	}

	private static void initBattleshipServerObjectPath() {
		try {
			properties.load(new FileInputStream(propertiesFilePath));
//			properties.load(ConfigReader.class.getClassLoader()
//					.getResourceAsStream(propertiesFilePath));
		} catch (IOException e1) {
			System.err.println("Error loading properties file: "
					+ propertiesFileName);
		}

		String ip = properties.getProperty("server.ip");
		String port = properties.getProperty("server.port");

		if (ip == null) {
			System.err.println("missing server.ip key");
			return;
		}

		if (port == null) {
			System.err.println("missing server.port key");
			return;
		}

		battleshipServerObjectPath = "rmi://" + ip + ":" + port + "/battleship";

	}

	public static void main(String[] args) {
		System.out.println(ConfigReader.getServerObjectPath());
	}

}
